{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>product_id</th>\n",
       "      <th>name</th>\n",
       "      <th>wholesale_price</th>\n",
       "      <th>retail_price</th>\n",
       "      <th>sales</th>\n",
       "      <th>current_net</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23</td>\n",
       "      <td>computer</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1000</td>\n",
       "      <td>100</td>\n",
       "      <td>50000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>96</td>\n",
       "      <td>Python Workout</td>\n",
       "      <td>35.0</td>\n",
       "      <td>75</td>\n",
       "      <td>1000</td>\n",
       "      <td>40000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>97</td>\n",
       "      <td>Pandas Workout</td>\n",
       "      <td>35.0</td>\n",
       "      <td>75</td>\n",
       "      <td>500</td>\n",
       "      <td>20000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15</td>\n",
       "      <td>banana</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1</td>\n",
       "      <td>200</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>87</td>\n",
       "      <td>sandwich</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>300</td>\n",
       "      <td>600.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   product_id            name  wholesale_price  retail_price  sales  \\\n",
       "0          23        computer            500.0          1000    100   \n",
       "1          96  Python Workout             35.0            75   1000   \n",
       "2          97  Pandas Workout             35.0            75    500   \n",
       "3          15          banana              0.5             1    200   \n",
       "4          87        sandwich              3.0             5    300   \n",
       "\n",
       "   current_net  \n",
       "0      50000.0  \n",
       "1      40000.0  \n",
       "2      20000.0  \n",
       "3        100.0  \n",
       "4        600.0  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame\n",
    "\n",
    "df = DataFrame([{'product_id':23, 'name':'computer', 'wholesale_price': 500, \n",
    "                 'retail_price':1000, 'sales':100},\n",
    "               {'product_id':96, 'name':'Python Workout', 'wholesale_price': 35,\n",
    "                'retail_price':75, 'sales':1000},\n",
    "               {'product_id':97, 'name':'Pandas Workout', 'wholesale_price': 35,\n",
    "                'retail_price':75, 'sales':500},\n",
    "               {'product_id':15, 'name':'banana', 'wholesale_price': 0.5,\n",
    "                'retail_price':1, 'sales':200},\n",
    "               {'product_id':87, 'name':'sandwich', 'wholesale_price': 3,\n",
    "                'retail_price':5, 'sales':300},\n",
    "               ])\n",
    "\n",
    "df['current_net'] = ((df['retail_price'] - df['wholesale_price']) * df['sales'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "An alternative tax plan would charge 25% tax, but only on those products on which we would net more than 20,000. In such a case, how much would we make?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "88200.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The short way, using lambda + the inline if-else\n",
    "df['current_net'].apply(lambda c: c*0.75 if c > 20000 else c).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "88200.0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The longer way, defining a \"real\" function with a normal if-else\n",
    "def calculate_tax(c):\n",
    "    if c > 20000:\n",
    "        return c * 0.75\n",
    "\n",
    "    return c\n",
    "\n",
    "df['current_net'].apply(calculate_tax).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "Yet another alternative tax plan would charge 25% tax on products whose retail price is greater than 80, 10% tax on products whose retail price is between 30 and 80, and no tax on others. Implement and calculate the result of such a tax scheme."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>product_id</th>\n",
       "      <th>name</th>\n",
       "      <th>wholesale_price</th>\n",
       "      <th>retail_price</th>\n",
       "      <th>sales</th>\n",
       "      <th>current_net</th>\n",
       "      <th>after_tax</th>\n",
       "      <th>final_net</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23</td>\n",
       "      <td>computer</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1000</td>\n",
       "      <td>100</td>\n",
       "      <td>50000.0</td>\n",
       "      <td>0.75</td>\n",
       "      <td>37500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>96</td>\n",
       "      <td>Python Workout</td>\n",
       "      <td>35.0</td>\n",
       "      <td>75</td>\n",
       "      <td>1000</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>0.90</td>\n",
       "      <td>36000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>97</td>\n",
       "      <td>Pandas Workout</td>\n",
       "      <td>35.0</td>\n",
       "      <td>75</td>\n",
       "      <td>500</td>\n",
       "      <td>20000.0</td>\n",
       "      <td>0.90</td>\n",
       "      <td>18000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15</td>\n",
       "      <td>banana</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1</td>\n",
       "      <td>200</td>\n",
       "      <td>100.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>87</td>\n",
       "      <td>sandwich</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>300</td>\n",
       "      <td>600.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>600.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   product_id            name  wholesale_price  retail_price  sales  \\\n",
       "0          23        computer            500.0          1000    100   \n",
       "1          96  Python Workout             35.0            75   1000   \n",
       "2          97  Pandas Workout             35.0            75    500   \n",
       "3          15          banana              0.5             1    200   \n",
       "4          87        sandwich              3.0             5    300   \n",
       "\n",
       "   current_net  after_tax  final_net  \n",
       "0      50000.0       0.75    37500.0  \n",
       "1      40000.0       0.90    36000.0  \n",
       "2      20000.0       0.90    18000.0  \n",
       "3        100.0       1.00      100.0  \n",
       "4        600.0       1.00      600.0  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Use pd.cut to set the cutoffs, then translate from category to floats\n",
    "df['after_tax'] = pd.cut(df['retail_price'], \n",
    "                   bins=[0, 30, 80, df['retail_price'].max()],\n",
    "                   labels=[1, 0.9, 0.75]).astype(np.float64)\n",
    "\n",
    "df['final_net'] = df['current_net'] * df['after_tax']\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "These long floating-point numbers are getting a bit hard to read. Set the `float_format` option in `pandas` such that the floating-point numbers will be displayed with commas every three digits before the decimal point, and only two digits after the decimal point. Note that this is a bit tricky, in that it requires understanding Python callables and the `str.format` method. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.display.float_format = '{:,.2f}'.format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>product_id</th>\n",
       "      <th>name</th>\n",
       "      <th>wholesale_price</th>\n",
       "      <th>retail_price</th>\n",
       "      <th>sales</th>\n",
       "      <th>current_net</th>\n",
       "      <th>after_tax</th>\n",
       "      <th>final_net</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23</td>\n",
       "      <td>computer</td>\n",
       "      <td>500.00</td>\n",
       "      <td>1000</td>\n",
       "      <td>100</td>\n",
       "      <td>50,000.00</td>\n",
       "      <td>0.75</td>\n",
       "      <td>37,500.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>96</td>\n",
       "      <td>Python Workout</td>\n",
       "      <td>35.00</td>\n",
       "      <td>75</td>\n",
       "      <td>1000</td>\n",
       "      <td>40,000.00</td>\n",
       "      <td>0.90</td>\n",
       "      <td>36,000.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>97</td>\n",
       "      <td>Pandas Workout</td>\n",
       "      <td>35.00</td>\n",
       "      <td>75</td>\n",
       "      <td>500</td>\n",
       "      <td>20,000.00</td>\n",
       "      <td>0.90</td>\n",
       "      <td>18,000.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15</td>\n",
       "      <td>banana</td>\n",
       "      <td>0.50</td>\n",
       "      <td>1</td>\n",
       "      <td>200</td>\n",
       "      <td>100.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>100.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>87</td>\n",
       "      <td>sandwich</td>\n",
       "      <td>3.00</td>\n",
       "      <td>5</td>\n",
       "      <td>300</td>\n",
       "      <td>600.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>600.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   product_id            name  wholesale_price  retail_price  sales  \\\n",
       "0          23        computer           500.00          1000    100   \n",
       "1          96  Python Workout            35.00            75   1000   \n",
       "2          97  Pandas Workout            35.00            75    500   \n",
       "3          15          banana             0.50             1    200   \n",
       "4          87        sandwich             3.00             5    300   \n",
       "\n",
       "   current_net  after_tax  final_net  \n",
       "0    50,000.00       0.75  37,500.00  \n",
       "1    40,000.00       0.90  36,000.00  \n",
       "2    20,000.00       0.90  18,000.00  \n",
       "3       100.00       1.00     100.00  \n",
       "4       600.00       1.00     600.00  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
